<template>
  <div class="register-page mx-auto p-3 max-w-330">
    <h5 class="my-4 text-center">注册新账号</h5>
    <validate-form @form-submit="onFormSubmit">
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input
          :rules="emailRules"
          v-model="emailVal"
          placeholder="请输入邮箱地址"
          type="text"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">昵称</label>
        <validate-input
          :rules="nameRules"
          v-model="nameVal"
          placeholder="请输入昵称"
          type="text"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input
          :rules="passwordRules"
          v-model="passwordVal"
          placeholder="请输入密码"
          type="password"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">重复密码</label>
        <validate-input
          :rules="repeatRules"
          v-model="repeatVal"
          placeholder="请输入重复密码"
          type="password"
        />
      </div>
      <template #submit>
        <button class="btn btn-primary">注册新账号</button>
      </template>
    </validate-form>
  </div>
</template>

<script lang='ts'>
import { defineComponent, ref } from 'vue'
import ValidateForm from '../components/ValidateForm.vue'
import ValidateInput, { RulesProp } from '../components/ValidateInput.vue'
import axios from '../libs/axios'
import createMessage from '../components/createMessage'
import { useRouter } from 'vue-router'

export default defineComponent({
  components: {
    ValidateForm,
    ValidateInput
  },
  setup () {
    const router = useRouter()

    const emailVal = ref('')
    const nameVal = ref('')
    const passwordVal = ref('')
    const repeatVal = ref('')

    const emailRules: RulesProp = [
      {
        type: 'required',
        message: '邮箱地址不能为空'
      },
      {
        type: 'email',
        message: '请输入正确的邮箱格式'
      }
    ]

    const nameRules: RulesProp = [
      {
        type: 'required',
        message: '昵称不能为空'
      }
    ]

    const passwordRules: RulesProp = [
      {
        type: 'required',
        message: '密码不能为空'
      },
      {
        type: 'custom',
        message: '密码不得小于六位数',
        validator () {
          return Promise.resolve(passwordVal.value.length >= 6)
        }
      }
    ]

    const repeatRules: RulesProp = [
      {
        type: 'required',
        message: '重复密码不能为空'
      },
      {
        type: 'custom',
        message: '重复密码不得小于六位数',
        validator () {
          return Promise.resolve(repeatVal.value.length >= 6)
        }
      },
      {
        type: 'custom',
        message: '重复密码与密码不一致',
        validator () {
          return Promise.resolve(passwordVal.value === repeatVal.value)
        }
      }
    ]

    const onFormSubmit = (verify: boolean) => {
      if (verify) {
        const data = {
          email: emailVal.value,
          nickName: nameVal.value,
          password: passwordVal.value
        }
        axios({
          url: '/users',
          data,
          method: 'post'
        }).then((res) => {
          if (res.data.code === 0) {
            createMessage('success', '注册成功，正在跳转至登录页面', 2000)
            setTimeout(() => {
              router.push({
                name: 'login'
              })
            }, 2000)
          }
        })
      }
    }

    return {
      emailVal,
      nameVal,
      passwordVal,
      repeatVal,
      emailRules,
      nameRules,
      passwordRules,
      repeatRules,
      onFormSubmit
    }
  }
})
</script>
